//
//  GameViewController.m
//  课余时间
//
//  Created by zgr on 16/4/6.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "GameViewController.h"
#import "UIColor+AddColor.h"
#import "GameCollectionViewCell.h"
#import "GameNewsViewController.h"

@interface GameViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,strong)UIView *moveView;
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)GameNewsViewController *gameNews;
@property (nonatomic,strong)UITapGestureRecognizer *tap;

@end

@implementation GameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 //   self.navigationController.navigationBar.translucent = NO;
    
    _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    
    _titleArray = @[@"最新",@"赛事",@"活动",@"视频",@"娱乐",@"官方",@"美女",@"攻略"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, KWIDTH, 30) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor luse];
    [_collectionView registerClass:[GameCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    // Do any additional setup after loading the view.
    self.moveView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    self.moveView.backgroundColor = [UIColor carrotColor];
    self.moveView.alpha = 0.5;
    [self.collectionView addSubview:_moveView];
    [self creatChildViewController];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titleArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GameCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    cell.label.text = _titleArray[indexPath.item];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 30);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row != 0) {
        [collectionView setContentOffset:CGPointMake(60 * indexPath.row - 60, 0) animated:YES];
    }
    [UIView transitionWithView:self.moveView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        self.moveView.frame = CGRectMake(60 * indexPath.row, 0, 50, 30);
         self.scrollView.contentOffset = CGPointMake(KWIDTH * indexPath.row, 0);
    } completion:nil];
   
    
}

#pragma mark ========= 添加子视图控制器的方法 =============
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view.superview.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    }];
    [self.view removeGestureRecognizer:tap];
}
-(void)edgePan:(UIScreenEdgePanGestureRecognizer *)sender
{
    CGPoint point = [sender locationInView:self.view.superview.superview];
    NSLog(@"%f",point.x);
    
    CGRect newFrame = self.view.superview.frame;
    switch (sender.state) {
        case UIGestureRecognizerStateEnded:
        {
            if (point.x < 100) {
                point.x = 0;
            }
            else
            {
                point.x = KWIDTH - 175;
                [self.view addGestureRecognizer:_tap];
            }
            newFrame.origin.x = point.x;
            [UIView animateWithDuration:0.5 animations:^{
                self.view.superview.frame = newFrame;
            }];
            
            
            
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            if (point.x > KWIDTH - 175) {
                point.x = KWIDTH - 175;
            }
            break;
        }
        default:
            break;
    }
    newFrame.origin.x = point.x ;
    self.view.superview.frame = newFrame;
}
-(void)creatChildViewController
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, KWIDTH, KHEIGHT - 50)];
    self.scrollView.contentSize = CGSizeMake(KWIDTH *_titleArray.count, KHEIGHT - 50);
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.backgroundColor = [UIColor pumpkinColor];
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];
    

        _gameNews = [[GameNewsViewController alloc]init];
        _gameNews.view.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT - 40);
        _gameNews.view.backgroundColor = [UIColor greenSeaColor];
    [_gameNews setViewControllerWithUrlSting:12 nextINdext:1 runString:GAME_NEWS_RUNIMG];
        
        [self addChildViewController:_gameNews];
        [self.scrollView addSubview:_gameNews.view];
        
        UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePan:)];
        edgePan.edges = UIRectEdgeLeft;
        [_gameNews.view addGestureRecognizer:edgePan];
 

    
   //  赛事
        GameNewsViewController *competition = [[GameNewsViewController alloc]init];
            competition.view.frame = CGRectMake(KWIDTH, 0, KWIDTH, KHEIGHT - 40);
            [competition setViewControllerWithUrlSting:73 nextINdext:1 runString:@"http://qt.qq.com/php_cgi/lol_mobile/gamecenter/varcache_gamecenterindex.php?plat=ios&version=9663&a1=qtapp&a2="];
            [self addChildViewController:competition];
            [self.scrollView addSubview:competition.view];
    


    // 活动
    GameNewsViewController *activity = [[GameNewsViewController alloc]init];
    activity.view.frame = CGRectMake(KWIDTH * 2, 0, KWIDTH, KHEIGHT -40);
    [activity setViewControllerWithUrlSting:23 nextINdext:1 runString:nil];
        [self addChildViewController:activity];
    [self.scrollView addSubview:activity.view];
    
    // 娱乐
    GameNewsViewController *entainment = [[GameNewsViewController alloc]init];
    entainment.view.frame = CGRectMake(KWIDTH * 4, 0, KWIDTH, KHEIGHT - 40);
    [entainment setViewControllerWithUrlSting:18 nextINdext:1 runString:nil];
    [self addChildViewController:entainment];
    [self.scrollView addSubview:entainment.view];
    
    // 官方
    GameNewsViewController *official = [[GameNewsViewController alloc]init];
    official.view.frame = CGRectMake(KWIDTH * 5, 0, KWIDTH, KHEIGHT - 40);
    [official setViewControllerWithUrlSting:3 nextINdext:1 runString:nil];
    [self addChildViewController:official];
    [self.scrollView addSubview:official.view];
    
    // 美女
    GameNewsViewController *belle = [[GameNewsViewController alloc]init];
    belle.view.frame = CGRectMake(KWIDTH * 6, 0, KWIDTH, KHEIGHT - 40);
    [belle setViewControllerWithUrlSting:17 nextINdext:1 runString:nil];
    [self addChildViewController:belle];
    [self.scrollView addSubview:belle.view];
    
    // 攻略
    GameNewsViewController *strategy = [[GameNewsViewController alloc]init];
    strategy.view.frame = CGRectMake(KWIDTH * 7, 0, KWIDTH, KHEIGHT - 40);
    [strategy setViewControllerWithUrlSting:10 nextINdext:1 runString:nil];
    [self addChildViewController:strategy];
    [self.scrollView addSubview:strategy.view];
  
 
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([_scrollView isEqual:scrollView]) {
        if (scrollView.contentOffset.x != 0) {
            [_collectionView setContentOffset:CGPointMake(60 * scrollView.contentOffset.x/KWIDTH - 60, 0) animated:YES];
        }
        else
        {
             [_collectionView setContentOffset:CGPointMake(60 * scrollView.contentOffset.x/KWIDTH , 0) animated:YES];
        }
        [UIView transitionWithView:_moveView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            _moveView.frame = CGRectMake(60 *self.scrollView.contentOffset.x/KWIDTH, 0, 50, 30);
        } completion:nil];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
